package cn.vortex.device.web.servlet;

import cn.vortex.device.service.UserService;
import cn.vortex.device.service.impl.UserServiceImpl;
import cn.vortex.device.tool.ResMessageBuilder;
import cn.vortex.device.vo.ResMessage;
import cn.vortex.device.vo.User;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "SystemServlet",urlPatterns = "/system/*")
public class SystemServlet extends BaseServlet {

    private UserService userService;

    public ResMessage login(HttpServletRequest request, HttpServletResponse response){


        userService = new UserServiceImpl();

        String check_code = (String) request.getSession().getAttribute("CHECK_CODE");
        String account = request.getParameter("account");
        String password = request.getParameter("password");
        String captcha = request.getParameter("captcha");

        if (check_code.equalsIgnoreCase(captcha)){

            return userService.userLogin(request,account,password);

        }else {
           return ResMessageBuilder.resMessage(200,"fail","验证码错误！");
        }

    }

    public String exit(HttpServletRequest request,HttpServletResponse response) throws IOException {

        request.getSession().removeAttribute("USER");

        return "r:/page/login.jsp";
    }

    public String categoryList(HttpServletRequest request,HttpServletResponse response){
        return "r:/page/category/categoryList.jsp";
    }

    public String addDevice(HttpServletRequest request,HttpServletResponse response){
        return "r:/page/device/addDevice.jsp";
    }

    public String deviceList(HttpServletRequest request,HttpServletResponse response){
        return "r:/page/device/deviceList.jsp";
    }

    public String downDevice(HttpServletRequest request,HttpServletResponse response){
        return "r:/page/device/downDevice.jsp";
    }


    public String downList(HttpServletRequest request,HttpServletResponse response){
        return "r:/page/device/downList.jsp";
    }

    public String fixDevice(HttpServletRequest request,HttpServletResponse response){
        return "r:/page/device/fixDevice.jsp";
    }

    public String fixList(HttpServletRequest request,HttpServletResponse response){
        return "r:/page/device/fixList.jsp";
    }
    public String fixInfoList(HttpServletRequest request,HttpServletResponse response){
        return "r:/page/device/fixInfoList.jsp";
    }

    public String addApply(HttpServletRequest request,HttpServletResponse response){
        return "r:/page/apply/addApply.jsp";
    }

    public String applyList(HttpServletRequest request,HttpServletResponse response){
        return "r:/page/apply/applyList.jsp";
    }

    public String editApply(HttpServletRequest request,HttpServletResponse response){
        return "r:/page/apply/editApply.jsp";
    }

    public String openApply(HttpServletRequest request,HttpServletResponse response){
        return "r:/page/apply/openApply.jsp";
    }

    public String checkApply(HttpServletRequest request,HttpServletResponse response){
        return "r:/page/apply/checkApply.jsp";
    }

    public String checkList(HttpServletRequest request,HttpServletResponse response){
        return "r:/page/apply/checkList.jsp";
    }

    public String console(HttpServletRequest request,HttpServletResponse response){
        return "r:/page/console.html";
    }

    public String addUser(HttpServletRequest request,HttpServletResponse response){
        return "r:/page/user/addUser.jsp";
    }

    public String editUser(HttpServletRequest request, HttpServletResponse response){
        Integer roleID = roleIdentify(request, response);
        if (roleID == 1){
            return "r:/page/user/editUserForUser.jsp";
        }else if (roleID == 2){
            return "r:/page/user/editUser.jsp";
        }
        return null;
    }

    public String openUser(HttpServletRequest request,HttpServletResponse response){
        return "r:/page/user/openUser.jsp";
    }

    public String userList(HttpServletRequest request,HttpServletResponse response){
        Integer roleID = roleIdentify(request, response);
        if (roleID == 1){
            return "r:/page/user/userListForUser.jsp";
        }else if (roleID == 2){
            return "r:/page/user/userList.jsp";
        }
        return null;
    }

    private Integer roleIdentify(HttpServletRequest request,HttpServletResponse response){
        Object user = request.getSession().getAttribute("USER");
        if (user != null){//判断user 是否存在
            if (user instanceof User){//判断user是否是User的实例化对象，是返回true
                User _user = (User) user;//将user转换为User对象
                return _user.getRoleId();
            }
            else {
                throw new RuntimeException("传入的USER不符合要求！");//如果user对象不是User类的实例化对象抛出异常
            }
        }
        else {
            throw new RuntimeException("传入的USER不符合要求！");
        }
    }




}
